package com.lagou.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: lcz
 * @date: Created in 9:42 2021/2/24
 * @description:
 */
@Configuration
public class RabbitConfig {

    @Bean
    public Queue queueTTL() {
        Map<String, Object> props = new HashMap<>();
        //设置该队列中的消息都等待10s中
        props.put("x-message-ttl", 10000);
        //设置该队列所关联的死信交换器（当队列消息TTL到期后依然没有消费，则加入死信队列）
        props.put("x-dead-letter-exchange", "ex.pay.dlx");
        //设置该队列所关联的死信交换器的routingKey，如果没有特殊指定，使用原队列的routingKey
        props.put("x-dead-letter-routing-key", "pay.dlx");
        Queue queue = new Queue("q.pay.ttl", true, false, false, props);
        return queue;
    }

    /**
     * @author: lcz
     * @date: Created in 11:29 2021/2/24
     * @description: 死信队列
     */
    @Bean
    public Queue queueDLX() {
        Queue queue = new Queue("q.pay.dlx", true, false, false);
        return queue;
    }

    @Bean
    public Exchange exchangeTTL() {
        DirectExchange exchange = new DirectExchange("ex.pay.ttl", true, false);
        return exchange;
    }

    /**
     * @author: lcz
     * @date: Created in 11:30 2021/2/24
     * @description: 死信交换器
     */
    @Bean
    public Exchange exchangeDLX() {
        DirectExchange exchange = new DirectExchange("ex.pay.dlx", true, false);
        return exchange;
    }

    @Bean
    public Binding bindingTTL() {
        Binding binding = BindingBuilder.bind(queueTTL()).to(exchangeTTL()).with("pay.ttl").noargs();
        return binding;
    }

    /**
     * @author: lcz
     * @date: Created in 11:29 2021/2/24
     * @description: 死信交换器绑定死信队列
     */
    @Bean
    public Binding bindingDLX() {
        Binding binding = BindingBuilder.bind(queueDLX()).to(exchangeDLX()).with("pay.dlx").noargs();
        return binding;
    }
}
